{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c92101f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from astropy.io import fits\n",
    "from reproject.mosaicking import find_optimal_celestial_wcs, reproject_and_coadd\n",
    "from reproject import reproject_interp\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "78b5934c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# List all the bone names\n",
    "ListNames = ['Fil1', 'Fil2', 'Fil4', 'Fil5', 'Fil8', 'Fil10', 'G24', 'G47', 'G49', 'Snake']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fef166f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current 24 um mosaic: Fil1\n",
      "0001_280.38466000_-5.50652000_MG0260n005_024.fits\n",
      "0001_280.38466000_-5.50652000_MG0260p005_024.fits\n",
      "0001_280.38466000_-5.50652000_MG0270n005_024.fits\n",
      "0001_280.38466000_-5.50652000_MG0270p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 26.499993703968432  -2.35233554351067e-05  \n",
      "CRPIX : 2606.7637664016556  2349.864150237355  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.00034722199999999995  0.00034722199999999995  \n",
      "NAXIS : 0  0\n",
      "(2884, 2883)\n",
      "Current 24 um mosaic: Fil2\n",
      "0001_279.69613000_-7.02544000_MG0250n005_024.fits\n",
      "0001_279.69613000_-7.02544000_MG0250p005_024.fits\n",
      "0001_279.69613000_-7.02544000_MG0260n005_024.fits\n",
      "0001_279.69613000_-7.02544000_MG0260p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 25.50001142829177  -3.1902601132063114e-05  \n",
      "CRPIX : 695.6066020472751  2601.882088408172  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.000347222  0.000347222  \n",
      "NAXIS : 0  0\n",
      "(2883, 2883)\n",
      "Current 24 um mosaic: Fil4\n",
      "0001_277.57516000_-10.46459000_MG0210n005_024.fits\n",
      "0001_277.57516000_-10.46459000_MG0210p005_024.fits\n",
      "0001_277.57516000_-10.46459000_MG0220n005_024.fits\n",
      "0001_277.57516000_-10.46459000_MG0220p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 21.499997979012402  8.14566083979809e-06  \n",
      "CRPIX : 642.5537572380348  1807.5960198938035  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.00034722199999999995  0.00034722199999999995  \n",
      "NAXIS : 0  0\n",
      "(2883, 2883)\n",
      "Current 24 um mosaic: Fil5\n",
      "0001_276.29150000_-12.80471000_MG0180n005_024.fits\n",
      "0001_276.29150000_-12.80471000_MG0180p005_024.fits\n",
      "0001_276.29150000_-12.80471000_MG0190n005_024.fits\n",
      "0001_276.29150000_-12.80471000_MG0190p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 18.50000170250973  -1.040218721368392e-05  \n",
      "CRPIX : 1632.6279972658558  1737.5145862303702  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.000347222  0.000347222  \n",
      "NAXIS : 0  0\n",
      "(2883, 2883)\n",
      "Current 24 um mosaic: Fil8\n",
      "0001_265.27311000_-31.22373000_MG3570n005_024.fits\n",
      "0001_265.27311000_-31.22373000_MG3570p005_024.fits\n",
      "0001_265.27311000_-31.22373000_MG3580n005_024.fits\n",
      "0001_265.27311000_-31.22373000_MG3580p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 357.4999951609738  -1.803973796820009e-05  \n",
      "CRPIX : 1553.7229504834165  2500.8250566158677  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.00034722199999999995  0.00034722199999999995  \n",
      "NAXIS : 0  0\n",
      "(2882, 2883)\n",
      "Current 24 um mosaic: Fil10\n",
      "0001_244.09313000_-50.85616000_MG3320n005_024.fits\n",
      "0001_244.09313000_-50.85616000_MG3320p005_024.fits\n",
      "0001_244.09313000_-50.85616000_MG3330n005_024.fits\n",
      "0001_244.09313000_-50.85616000_MG3330p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 332.50000128457407  6.545593876255234e-07  \n",
      "CRPIX : 1204.4808047387319  1748.618016698298  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.00034722199999999995  0.00034722199999999995  \n",
      "NAXIS : 0  0\n",
      "(2883, 2884)\n",
      "Current 24 um mosaic: G24\n",
      "0001_278.24577000_-7.83358000_MG0230n005_024.fits\n",
      "0001_278.24577000_-7.83358000_MG0230p005_024.fits\n",
      "0001_278.24577000_-7.83358000_MG0240n005_024.fits\n",
      "0001_278.24577000_-7.83358000_MG0240p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 23.500007094435876  -1.9242421897265008e-06  \n",
      "CRPIX : 2483.8531172798803  -5.667812569511659  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.00034722199999999995  0.00034722199999999995  \n",
      "NAXIS : 0  0\n",
      "(2883, 2884)\n",
      "Current 24 um mosaic: G47\n",
      "0001_289.17399000_12.74481000_MG0470n005_024.fits\n",
      "0001_289.17399000_12.74481000_MG0470p005_024.fits\n",
      "0001_289.17399000_12.74481000_MG0480n005_024.fits\n",
      "0001_289.17399000_12.74481000_MG0480p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 47.49999995526153  -9.201028924880082e-06  \n",
      "CRPIX : 373.54426251498666  596.6258888596165  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.000347222  0.000347222  \n",
      "NAXIS : 0  0\n",
      "(2883, 2883)\n",
      "Current 24 um mosaic: G49\n",
      "0001_290.65823000_14.17855000_MG0490n005_024.fits\n",
      "0001_290.65823000_14.17855000_MG0490p005_024.fits\n",
      "0001_290.65823000_14.17855000_MG0500n005_024.fits\n",
      "0001_290.65823000_14.17855000_MG0500p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 49.50000446974706  4.71542389915583e-06  \n",
      "CRPIX : 209.65047728467016  2332.8597280607396  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.000347222  0.000347222  \n",
      "NAXIS : 0  0\n",
      "(2884, 2883)\n",
      "Current 24 um mosaic: Snake\n",
      "0001_272.61988000_-19.34150000_MG0110n005_024.fits\n",
      "0001_272.61988000_-19.34150000_MG0110p005_024.fits\n",
      "0001_272.61988000_-19.34150000_MG0120n005_024.fits\n",
      "0001_272.61988000_-19.34150000_MG0120p005_024.fits\n",
      "WCS Keywords\n",
      "\n",
      "Number of WCS axes: 2\n",
      "CTYPE : 'GLON-TAN'  'GLAT-TAN'  \n",
      "CRVAL : 11.500023087358564  8.980244153162835e-06  \n",
      "CRPIX : 402.6323382727286  1728.6151756883608  \n",
      "PC1_1 PC1_2  : 1.0  0.0  \n",
      "PC2_1 PC2_2  : 0.0  1.0  \n",
      "CDELT : -0.00034722199999999995  0.00034722199999999995  \n",
      "NAXIS : 0  0\n",
      "(2883, 2882)\n"
     ]
    }
   ],
   "source": [
    "for Name in ListNames:\n",
    "    # Reference and Output directories for the bone\n",
    "    print('Current 24 um mosaic: '+Name)\n",
    "    RefDir = 'Spitzer/'+Name+'/24um/'\n",
    "    OutDir = 'Spitzer/'+Name+'/'\n",
    "    # Reading the names of all the 24  um files for the bone\n",
    "    RefFiles = []\n",
    "    for entry in os.scandir(RefDir):\n",
    "        if entry.is_file() and entry.name.endswith('.fits'):\n",
    "            print(entry.name)\n",
    "            RefFiles.append(entry.name)\n",
    "    # Opening the FITS containers\n",
    "    fitsList = []\n",
    "    for RefFile in RefFiles:\n",
    "        fitsList.append(fits.open(RefDir+RefFile))\n",
    "    # Caculating the optimal WCS object\n",
    "    wcs_out, shape_out = find_optimal_celestial_wcs(fitsList,frame='galactic')\n",
    "    print(wcs_out)\n",
    "    print(shape_out)\n",
    "    # Mosaic the fits files using the new WCS information\n",
    "    NewData, footprint = reproject_and_coadd(fitsList,\n",
    "                                             wcs_out, shape_out=shape_out,\n",
    "                                             reproject_function=reproject_interp)\n",
    "    # Creating the new FITS file\n",
    "    header = wcs_out.to_header()\n",
    "    NewFITS = fits.PrimaryHDU(data=NewData,header=header)\n",
    "    # Saving the fits file\n",
    "    NewFITS.writeto(OutDir+Name+'_24um.fits', overwrite=True)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
